package system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import common.model.po.SysUpdateLog;
import common.model.request.base.PaginationRequest;
import common.model.response.base.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import system.mapper.SysUpdateLogMapper;
import system.model.converter.SysUpdateLogConverter;
import system.model.request.SaveUpdateLogRequest;
import system.model.request.SearchUpdateLogRequest;
import system.service.SysUpdateLogService;

import javax.annotation.Resource;

/**
 * 服务实现类
 *
 * @author 米泽鹏
 * @since 2024-01-13 下午 07:21
 */
@Service
public class SysUpdateLogServiceImpl extends ServiceImpl<SysUpdateLogMapper, SysUpdateLog> implements SysUpdateLogService {

	@Resource
	private SysUpdateLogMapper mapper;

	@Override
	public Page<SysUpdateLog> pageUpdateLog(PaginationRequest<SearchUpdateLogRequest, SysUpdateLog> paginationRequest) {
		LambdaQueryWrapper<SysUpdateLog> queryWrapper = paginationRequest.buildQueryWrapper().lambda();
		// 排序
		queryWrapper.orderByDesc(SysUpdateLog::getReleaseTime);
		return page(paginationRequest.buildPage(), queryWrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Result<?> saveUpdateLog(SaveUpdateLogRequest request) {
		SysUpdateLog po = SysUpdateLogConverter.saveRequestToPo(request);
		if (po.getId() == null) {
			save(po);
		} else {
			updateById(po);
		}
		return Result.success();
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Result<?> deleteUpdateLog(Long id) {
		removeById(id);
		return Result.success();
	}

}
